#include<bits/stdc++.h>
#define LL long long
using namespace std;
    int T;
    int n;
    LL ans;
    bool h[1000006];
    LL f[1000006];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    int x,lastx,maxx;
    LL s,lastf;
    while(T--){
        ans=0;maxx=0;s=0;lastf=0;
        memset(f,0,sizeof(f));
        memset(h,0,sizeof(h));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            maxx=max(maxx,x);
            if(i==1){
                h[x]=1;
                lastx=x;
                continue;
            }
            if(x==lastx){
                s+=x;
                continue;
            }
            f[lastx]=lastf;
            if(h[x])f[lastx]=max(f[x]+x,f[lastx]),lastf=max(lastf,f[lastx]);
            h[x]=1;
            lastx=x;
        }
        for(int i=1;i<=maxx;i++)ans=max(ans,f[i]);
        printf("%lld\n",ans+s);
    }
    return 0;
}